<template>
  <el-drawer
    size="75%"
    :title="drawer.title"
    :visible.sync="drawer.open"
    :direction="direction"
    :before-close="handleClose"
  >
    <div style="padding: 0px 20px">
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="120px"
        class="column-form"
        :style="{ overflow: 'auto', height: fromHeight + 'px' }"
      >
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="归属栏目" prop="columnId">
              <el-cascader
                v-model="formData.columnId"
                :options="columnIdOptions"
                :props="columnIdProps"
                :style="{ width: '100%' }"
                placeholder="请选择归属栏目"
                clearable
              ></el-cascader>
            </el-form-item>
            <el-row>
              <el-col :span="22">
                <el-form-item label="文章标题" prop="title">
                  <el-input
                    v-model="formData.title"
                    placeholder="请输入文章标题"
                    :maxlength="50"
                    show-word-limit
                    clearable
                    :style="{ width: '100%' }"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-color-picker
                  v-model="formData.titleColor"
                  style="float: right"
                ></el-color-picker>
              </el-col>
            </el-row>
            <el-form-item label="副标题" prop="subTitle">
              <el-input
                v-model="formData.subTitle"
                placeholder="请输入副标题"
                :maxlength="100"
                show-word-limit
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
            <el-form-item label="关键词" prop="keyWord">
              <el-input
                v-model="formData.keyWord"
                type="textarea"
                placeholder="请输入SEO关键词"
                :maxlength="200"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 4 }"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>

            <el-form-item label="描述" prop="summary">
              <el-input
                v-model="formData.summary"
                type="textarea"
                placeholder="请输入SEO描述"
                :maxlength="300"
                show-word-limit
                :autosize="{ minRows: 2, maxRows: 4 }"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
            <el-form-item label="外链地址" prop="linkUrl">
              <el-input
                v-model="formData.linkUrl"
                placeholder="请输入外链地址"
                :maxlength="255"
                show-word-limit
                clearable
                prefix-icon="el-icon-link"
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
            <el-form-item label="文章标签" prop="tag">
              <!-- <el-input
              v-model="formData.tag"
              placeholder="请输入文章标签"
              :maxlength="100"
              show-word-limit
              clearable
              :style="{ width: '100%' }"
            ></el-input> -->
              <el-select
                v-model="formData.tag"
                multiple
                filterable
                allow-create
                clearable
                :maxlength="100"
                placeholder="手动输入文章标签"
                :style="{ width: '100%' }"
              ></el-select>
            </el-form-item>
            <el-form-item label="权重" prop="sort">
              <el-slider
                v-model="formData.sort"
                placeholder="请输入作者"
                clearable
                :max="500"
                :style="{ width: '100%' }"
                show-input
              ></el-slider>
            </el-form-item>
            <el-row style="padding-left: 40px">
              <el-col :span="6">
                <el-form-item label="总点击量" label-width="80px" prop="hits">
                  <el-input
                    v-model="formData.hits"
                    placeholder="请输入总点击量"
                    clearable
                    :style="{ width: '100%' }"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="日点击量"
                  label-width="80px"
                  prop="dayHits"
                >
                  <el-input
                    v-model="formData.dayHits"
                    placeholder="日点击量"
                    clearable
                    :style="{ width: '100%' }"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="周点击量"
                  label-width="80px"
                  prop="weedHits"
                >
                  <el-input
                    v-model="formData.weedHits"
                    placeholder="周点击量"
                    clearable
                    :style="{ width: '100%' }"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="月点击量"
                  label-width="80px"
                  prop="monthHits"
                >
                  <el-input
                    v-model="formData.monthHits"
                    placeholder="月点击量"
                    clearable
                    :style="{ width: '100%' }"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布时间" prop="updateTime">
              <el-date-picker
                v-model="formData.updateTime"
                type="datetime"
                :style="{ width: '100%' }"
                clearable
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="审核状态" prop="status" required>
              <el-switch v-model="formData.status"></el-switch>
            </el-form-item>
            <el-form-item label="上传" prop="imgUrl">
              <el-upload
                ref="imgUrl"
                :file-list="imgUrlfileList"
                :action="imgUrlAction"
                :before-upload="imgUrlBeforeUpload"
                list-type="picture-card"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="多选框组" prop="attr">
              <el-checkbox-group v-model="formData.attr" size="medium">
                <el-checkbox
                  v-for="(item, index) in attrOptions"
                  :key="index"
                  :label="item.value"
                  :disabled="item.disabled"
                >
                  {{ item.label }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>

            <el-form-item label="作者" prop="author">
              <el-input
                v-model="formData.author"
                placeholder="请输入作者"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
            <el-form-item label="来源" prop="source">
              <el-input
                v-model="formData.source"
                placeholder="请输入来源"
                :maxlength="200"
                clearable
                :style="{ width: '100%' }"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24"></el-col>
        </el-row>
        <el-form-item label="文章内容" prop="content">
          <tinymce-editor v-model="formData.content"></tinymce-editor>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="drawer.btnLoading"
            @click="submitForm"
          >
            保存文章
          </el-button>
          <el-button type="warning" @click="close">返回上一步</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>
<script>
  import { add, update, getColumn } from '@/api/cms/article'
  import { changeTree } from '@/utils/treeTool'
  import { deepClone } from '@/utils/index'
  import TinymceEditor from '@/components/tinymce-editor/tinymce-editor'
  export default {
    name: 'CmsColumnModify',
    components: { TinymceEditor },
    data() {
      return {
        drawer: {
          open: false,
          title: '添加栏目',
          btnLoading: false,
        },
        fromHeight: window.innerHeight - 100,
        direction: 'rtl',
        formData: {
          parent: [],
          title: undefined,
          titleColor: undefined,
          columnId: undefined,
          subTitle: undefined,
          updateTime: new Date(),
          keyWord: undefined,
          summary: undefined,
          linkUrl: undefined,
          status: true,
          tag: undefined,
          attr: [],
          author: undefined,
          source: undefined,
          sort: 0,
          hits: 0,
          dayHits: 0,
          weedHits: 0,
          monthHits: 0,
        },
        rules: {
          title: [
            {
              required: true,
              message: '请输入文章标题',
              trigger: 'blur',
            },
          ],
          columnId: [
            {
              required: true,
              type: 'array',
              message: '请至少选择一个归属栏目',
              trigger: 'change',
            },
          ],
          subTitle: [],
          updateTime: [
            {
              required: true,
              message: '发布时间不能为空',
              trigger: 'change',
            },
          ],
          keyWord: [
            {
              required: true,
              message: '请输入SEO关键词',
              trigger: 'blur',
            },
          ],
          summary: [
            {
              required: true,
              message: '请输入SEO描述',
              trigger: 'blur',
            },
          ],
          linkUrl: [],
          tag: [
            {
              required: true,
              message: '请输入文章标签',
              trigger: 'blur',
            },
          ],
          attr: [],
          sort: [],
          author: [],
          source: [],
          hits: [],
          dayHits: [],
          weedHits: [],
          monthHits: [],
        },
        attrOptions: [
          {
            label: '是否推荐',
            value: 1,
          },
          {
            label: '是否热点',
            value: 2,
          },
          {
            label: '是否滚动',
            value: 3,
          },
          {
            label: '是否评论',
            value: 4,
          },
          {
            label: '是否回收站',
            value: 5,
          },
        ],
        imgUrlAction: 'https://jsonplaceholder.typicode.com/posts/',
        imgUrlfileList: [],
        columnIdOptions: [],
        columnIdProps: {
          multiple: false,
          checkStrictly: true,
          expandTrigger: 'hover',
          label: 'label',
          value: 'value',
          children: 'children',
        },
        columnList: [],
      }
    },
    created() {
      //this.initTree()
    },
    mounted() {
      tinymce.init({})
    },
    methods: {
      async initTree() {
        const t = await getColumn()
        this.columnList = t.data
        let _tree = [{ id: '1', value: '0', label: '根目录', parentId: '0' }]
        t.data.some((m) => {
          _tree.push({
            id: m.id,
            value: m.id,
            label: m.title,
            parentId: m.parentId,
          })
        })
        this.columnIdOptions = changeTree(_tree)
        if (this.formData.id) {
          let columnModel = t.data.filter((m) => m.id == this.formData.columnId)
          var str = columnModel[0].parentIdList.split(',')
          str.forEach(function (item, i) {
            if (item != columnModel[0].id && item) {
              this.formData.columnId.parent.push(item)
            }
            if (columnModel[0].parentId == '0' && item) {
              this.formData.columnId.parent.push('0')
            }
          })
        }
      },
      close() {
        this.$refs['elForm'].resetFields()
        this.drawer.open = false
      },
      imgUrlBeforeUpload(file) {
        let isRightSize = file.size / 1024 / 1024 < 2
        if (!isRightSize) {
          this.$message.error('文件大小超过 2MB')
        }
        return isRightSize
      },
      submitForm() {
        this.$refs['elForm'].validate(async (valid) => {
          if (!valid) return
          //console.log(this.formData)
          //return
          this.drawer.btnLoading = true
          this.formData.columnId = this.formData.columnId.pop()
          let tipName = '添加成功'
          let res = null
          if (!this.formData.id) {
            this.formData.id = undefined
            res = await add(this.formData)
          } else {
            tipName = '修改成功'
            res = await update(this.formData)
          }
          this.drawer.btnLoading = false
          if (res.code == 200) {
            this.close()
            this.$emit('complete')
            this.$notify({
              message: tipName,
              type: 'success',
            })
          } else {
            this.$notify({
              message: message,
              type: 'error',
            })
          }
        })
      },
      resetForm() {
        this.$refs['elForm'].resetFields()
      },
      handleAdd() {
        this.drawer.open = true
        this.drawer.title = '添加文章'
        this.initTree()
      },
      handelModify(record) {
        this.initTree()
        this.drawer.open = true
        this.drawer.title = '编辑文章'
        this.$nextTick(() => {
          this.formData = deepClone(record)
          this.formData.attr = []
          record.attr.forEach((m) => {
            this.formData.attr.push(parseInt(m))
          })
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then((_) => {
            this.$refs['elForm'].resetFields()
            done()
          })
          .catch((_) => {})
      },
    },
  }
</script>
<style>
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .column-form {
    padding-right: 20px;
  }
</style>
